package com.tanhua.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.domain.vo.SoundVo;
import com.tanhua.dubbo.api.mongo.SoundApi;
import com.tanhua.server.Interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class SoundService {
    @Autowired
    private FastFileStorageClient client;
    @Autowired
    private FdfsWebServer server;
    @Reference
    private SoundApi soundApi;
    public void saveSound(MultipartFile soundFile) throws IOException {
        String originalFilename = soundFile.getOriginalFilename();
        String file = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
        StorePath storePath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), file, null);
        String path=server.getWebServerUrl()+"/"+storePath.getFullPath();
        soundApi.saveSound(UserHolder.getUserId(),path);
    }

    public ResponseEntity getSound() {
      SoundVo soundVo= soundApi.getSound(UserHolder.getUserId());
      return ResponseEntity.ok(soundVo);
    }
}
